<?php
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\Session;
use app\home\controller\base;
class Shopcar extends base
{

    public function delgoods(){
        $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
        $id=$_GET['goods_id'];
        $res=Db::execute("update shopcar set   status='1' where goods_id='$id' and user_id='$user_id'");
        echo "<script>alert('删除成功');location.href='http://www.jodieme.cn/index.php/home/shopcar/car'</script>";
    }

    public function delall(){
        $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
        $res=Db::execute("update shopcar set   status='1' where user_id='$user_id'");
        echo "<script>alert('删除成功');location.href='http://www.jodieme.cn/index.php/home/shopcar/car'</script>";
    }

    public function car()
    {
        $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
    	$res=Db::query("select * from shopcar where status='0' and user_id='$user_id' ");
    	$count=0;
		foreach ($res as $key => $value) {
			$count+=$value['car_num']*$value['market_price'];
		}
		$goods_count=0;
		foreach ($res as $key => $value) {
			$goods_count+=$value['goods_price']*$value['car_num'];
		}
		$save_price=$goods_count-$count;

    	$this->assign('save_price',$save_price);
    	$this->assign('goods_count',$goods_count);
     	$this->assign('res',$res);
      	$this->assign('count',$count);
    	return $this->fetch();
    }

    public function change()
    {
        $user_info = session::get("user_info");
        $user_id = $user_info['user_id']; 
        $goods_id=input('get.goods_id');
        $goods_number_44 = input("get.goods_number_44");
        $res=Db::query("update shopcar set car_num='$goods_number_44' where status='0' and goods_id='$goods_id' and user_id='$user_id' ");
        echo json_encode(1);
    }

    public function car_add()
    {
        $goods_id=input('post.goods_id');
        $number=input('post.number');

            $data=Db('goods')->where('goods_id',$goods_id)->find();
            $user_info = session::get("user_info");
        $user_id = $user_info['user_id']; 
        $pricesum=$number*$data['shop_price'];
        $res=Db::query("select * from shopcar where status='0' and goods_id='$goods_id' and user_id='$user_id' ");
        if($res){      
             $market_price=($number+$res[0]['car_num'])*$data['shop_price'];
             // $res2=Db::execute("update shopcar set  market_price='$market_price' where goods_id='$goods_id' and user_id='$user_id' and status='0' ");
             $res3=Db::execute("update shopcar set  car_num=car_num+$number where goods_id='$goods_id' and user_id='$user_id' and status='0' ");
        }else{
            $arr=[
                        'user_id'=>$user_id,
                        'goods_id'=>$goods_id,
                        'goods_name'=>$data['goods_name'],
                        'goods_price'=>$data['market_price'],
                        'car_num'=>$number,
                        'market_price'=>$pricesum,
                        'goods_img'=>$data['goods_img']
                    ];
            $res4=Db('shopcar')->insert($arr);
        }
        echo "<script>alert('添加购物车成功');location.href='http://www.jodieme.cn/index.php/home/shopcar/car'</script>";
 
    }	
    public function checkout()
    {
        $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
    	$res=Db::query("select * from shopcar where status='0' and user_id='$user_id' ");
     	$count=0;
		foreach ($res as $key => $value) {
			$count+=$value['car_num']*$value['market_price'];
		}
		$goods_count=0;
		foreach ($res as $key => $value) {
			$goods_count+=$value['goods_price']*$value['car_num'];
		}
		$save_price=$goods_count-$count;
		$data=Db('recipient')->select();
		$this->assign('data',$data);
		$this->assign('save_price',$save_price);
    	$this->assign('goods_count',$goods_count);
     	$this->assign('res',$res);
      	$this->assign('count',$count);
    	return $this->fetch();
    }
    public function order_add()
    {  
    	$user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
    	$data=input('post.');
    	
    	$address_data=Db('recipient')->where('id',$data['recipient_id'])->find();
    	// var_dump($address_data);die;
    	$shipping_arr=array('申通快递','邮局平邮');
	    $pay_arr=array('余额支付','银行亏款/转账','货到付款','支付宝');
	    $car_data=Db('shopcar')->where('user_id='.$user_id)->select();
	    // var_dump($car_data);die;
	    $arr=array(
             'order_sn'=>$this->createsn(),
             'user_id'=>$user_id,
             'order_status'=>0,
             'pay_status'=>0,
             'order_email'=>$address_data['email'],
             'postcode'=>$address_data['zipcode'],
             'consignee'=>$address_data['consignee'],
             'country'=>$address_data['country'],
             'province'=>$address_data['province'],
             'city'=>$address_data['city'],
             'district'=>$address_data['district'],
             'order_address'=>$address_data['address'],
             'tel'=>$address_data['tel'],
             // 'shipping_id'=>$data['ch'],
             'shipping_name'=>$shipping_arr[$data['shipping']],
             // 'pay_id'=>$data['payment'],
             'pay_name'=>$pay_arr[$data['payment']],  
             'order_amount'=>$data['sumprice'],
             'addtime'=>time()
        );
        $order=Db('order')->insert($arr);
        $order_id=Db('order')->getLastInsID();
        // var_dump($order_id);die;

        $tmp=Db('order')->where('order_id='.$order_id)->find();
         
        if ($order) {
        	foreach ($car_data as $key => $value) {
        		$car_data[$key]['order_id']=$order_id;
        		unset($car_data[$key]['car_id']);
        		unset($car_data[$key]['user_id']);
        		unset($car_data[$key]['goods_img']);
                unset($car_data[$key]['status']);

        	}
        		// var_dump($car_data);die;
        		$res=Db('goods_order')->insertAll($car_data);
        	if ($res) {
        		Db('shopcar')->where('user_id',$user_id)->delete();
        	}
        }
        //$res=Db::execute("update shopcar set  status='1' where user_id='$user_id'");
        echo "<script>alert('开心吗、没付钱就买东西');location.href='http://www.jodieme.cn/index.php/home/shopcar/car'</script>";

    }
    public function createsn()
    {
        return date('YmdHis').rand(100000,999999);
    }

}